﻿Public Class operations

    Public Function get_files(ByRef dir As String) As Integer

        Dim file As String = Nothing
        Dim attribute As System.IO.FileAttributes = Nothing
        Dim h As Integer = 0
        Dim a As Integer = 0
        Dim s As Integer = 0
        Dim r As Integer = 0

        Try
            For Each file In System.IO.Directory.GetFiles(dir, "*", System.IO.SearchOption.AllDirectories)

                get_files += 1
                attribute = System.IO.File.GetAttributes(file)

                If attribute.ToString.Contains("Hidden") Then
                    h += 1
                End If

                If attribute.ToString.Contains("Archive") Then
                    a += 1
                End If

                If attribute.ToString.Contains("System") Then
                    s += 1
                End If

                If attribute.ToString.Contains("ReadOnly") Then
                    r += 1
                End If

                Application.DoEvents()

            Next

            frm_main.txt_fi_a.Text = a.ToString
            frm_main.txt_fi_h.Text = h.ToString
            frm_main.txt_fi_s.Text = s.ToString
            frm_main.txt_fi_r.Text = r.ToString

            Return get_files
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Function

    Public Function get_folders(ByRef dir As String) As Integer
        Dim folder As String = Nothing

        Dim attribute As System.IO.FileAttributes = Nothing
        Dim h As Integer = 0
        Dim a As Integer = 0
        Dim s As Integer = 0
        Dim r As Integer = 0
        Try
            For Each folder In System.IO.Directory.GetDirectories(dir, "*", System.IO.SearchOption.AllDirectories)

                attribute = System.IO.File.GetAttributes(folder)

                If attribute.ToString.Contains("Hidden") Then
                    h += 1
                End If

                If attribute.ToString.Contains("Archive") Then
                    a += 1
                End If

                If attribute.ToString.Contains("System") Then
                    s += 1
                End If

                If attribute.ToString.Contains("ReadOnly") Then
                    r += 1
                End If

                get_folders += 1

                Application.DoEvents()

            Next

            frm_main.txt_fo_a.Text = a.ToString
            frm_main.txt_fo_h.Text = h.ToString
            frm_main.txt_fo_s.Text = s.ToString
            frm_main.txt_fo_r.Text = r.ToString

            Return get_folders

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Function

    Public Sub apply_atrributes(ByRef dir As String, ByRef RO As Boolean, _
                                ByVal H As Boolean, ByVal S As Boolean, _
                                ByVal A As Boolean, ByVal type As Integer, ByVal recursive As Boolean)
        Dim f As String = Nothing

        Dim attributes As IO.FileAttributes = IO.FileAttributes.Normal

        Dim files As Integer = 0
        Dim folders As Integer = 0
        Try
            If H = True Then
                attributes = CType(attributes + IO.FileAttributes.Hidden, IO.FileAttributes)
            End If
            If RO = True Then
                attributes = CType(attributes + IO.FileAttributes.ReadOnly, IO.FileAttributes)
            End If
            If S = True Then
                attributes = CType(attributes + IO.FileAttributes.System, IO.FileAttributes)
            End If
            If A = True Then
                attributes = CType(attributes + IO.FileAttributes.Archive, IO.FileAttributes)
            End If

            If recursive = True Then

                Select Case type
                    Case Is = 0
                        For Each f In System.IO.Directory.GetFiles(dir, "*", System.IO.SearchOption.AllDirectories)
                            System.IO.File.SetAttributes(f, attributes)
                            files += 1
                        Next
                    Case Is = 1
                        For Each f In System.IO.Directory.GetDirectories(dir, "*", System.IO.SearchOption.AllDirectories)
                            System.IO.File.SetAttributes(f, attributes)
                            folders += 1
                        Next
                    Case Is = 2
                        For Each f In System.IO.Directory.GetFiles(dir, "*", System.IO.SearchOption.AllDirectories)
                            System.IO.File.SetAttributes(f, attributes)
                            files += 1
                        Next

                        For Each f In System.IO.Directory.GetDirectories(dir, "*", System.IO.SearchOption.AllDirectories)
                            System.IO.File.SetAttributes(f, attributes)
                            folders += 1
                        Next
                End Select

            Else

                Select Case type
                    Case Is = 0
                        For Each f In System.IO.Directory.GetFiles(dir, "*", System.IO.SearchOption.TopDirectoryOnly)
                            System.IO.File.SetAttributes(f, attributes)
                            files += 1
                        Next
                    Case Is = 1
                        For Each f In System.IO.Directory.GetDirectories(dir, "*", System.IO.SearchOption.TopDirectoryOnly)
                            System.IO.File.SetAttributes(f, attributes)
                            folders += 1
                        Next
                    Case Is = 2

                        For Each f In System.IO.Directory.GetFiles(dir, "*", System.IO.SearchOption.TopDirectoryOnly)
                            System.IO.File.SetAttributes(f, attributes)
                            files += 1
                        Next

                        For Each f In System.IO.Directory.GetDirectories(dir, "*", System.IO.SearchOption.TopDirectoryOnly)
                            System.IO.File.SetAttributes(f, attributes)
                            folders += 1
                        Next

                End Select

            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

        frm_main.txt_applied.Text = "Folder attributes changed: " & folders.ToString & "  " & "File attributes changed: " & files.ToString
    End Sub

End Class
